

import axios from "axios";
import {message} from "antd"
import {history} from "./"
const baseURL = "http://47.94.244.28:3015";   // 开发环境 develoment 
// const baseURL = "http://47.104.209.44:3015";   // 线上环境 production 

let token = "";

axios.defaults.baseURL = baseURL;   // 使用代理  不准使用 基路径 
axios.defaults.headers.common['token'] = token;  // 设置请求头里面的 token 
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; // 设置POST提交数据的文档类型编码

let loading = null;
function showLoading(){
    message.destroy()
    loading = message.loading("加载中...")
}
function showSuccess(msg){
    message.destroy();
    message.success(msg)
}
function showError(msg){
    message.destroy();
    message.error(msg)
}

// 拦截器

axios.interceptors.request.use(function (config) {
    config.headers.token=sessionStorage.token;
    showLoading();
    return config;
  }, function (error) {
    showError("请求失败");
    return Promise.reject(error);
});


axios.interceptors.response.use(function (response) {
    
    if(response.data.type==1){
        showSuccess(response.data.msg||'请求成功');
    }else{
        if(response.data.type==0){
            showError(response.data.msg||'请求失败');
        }else{
            showSuccess(response.data.msg||'请求成功');
        }
    }
    if(response.data.code == 3000){  //  token 失效 就去重新登录 
       history.push("/login");
       window.history.go(0);
    }
    return response;
  }, function (error) {
    showError("响应失败");
    return Promise.reject(error);
});


export {axios,baseURL};